<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>加密工具增强版</title>
		<script src="js/crypto-js.min.js"></script>
		<script src="js/sm2.js"></script>
		<script src="js/sm3.js"></script>
		<script src="js/sm4.js"></script>
		<style>
			body {
				font-family: Arial, sans-serif;
				max-width: 800px;
				margin: 0 auto;
				padding: 20px;
				line-height: 1.6;
			}

			h1 {
				color: #333;
				text-align: center;
			}

			.container {
				background-color: #f9f9f9;
				border-radius: 8px;
				padding: 20px;
				box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
			}

			textarea {
				width: 100%;
				height: 100px;
				padding: 10px;
				margin-bottom: 15px;
				border: 1px solid #ddd;
				border-radius: 4px;
				resize: vertical;
			}

			select,
			button {
				padding: 10px 15px;
				margin-right: 10px;
				margin-bottom: 15px;
				border: 1px solid #ddd;
				border-radius: 4px;
				background-color: #fff;
				cursor: pointer;
			}

			button {
				background-color: #4CAF50;
				color: white;
				border: none;
				transition: background-color 0.3s;
			}

			button:hover {
				background-color: #45a049;
			}

			.result {
				margin-top: 20px;
				padding: 15px;
				background-color: #e9e9e9;
				border-radius: 4px;
				word-wrap: break-word;
			}

			.key-input {
				width: 100%;
				padding: 10px;
				margin-bottom: 15px;
				border: 1px solid #ddd;
				border-radius: 4px;
			}

			.info {
				font-size: 0.9em;
				color: #666;
				margin-top: 5px;
			}
		</style>
	</head>
	<body>
		<div class="container">
			<h1>多种加密方式工具</h1>

			<div>
				<label for="inputText">输入文本:</label>
				<textarea id="inputText" placeholder="请输入要加密的文本..."></textarea>
			</div>

			<div>
				<label for="encryptionMethod">加密方式:</label>
				<select id="encryptionMethod">
					<option value="md5">MD5</option>
					<option value="sha256">SHA-256</option>
					<option value="aes">AES</option>
					<option value="sm3">SM3 (国密)</option>
					<option value="sm4">SM4 (国密)</option>
					<option value="sm2">SM2 (国密)</option>
				</select>

				<div id="keyContainer" style="display: none;">
					<label for="keyInput">密钥 (可选):</label>
					<input type="text" id="keyInput" class="key-input" placeholder="请输入密钥...">
					<div class="info">对于AES/SM4，密钥长度应为16/24/32字节；对于SM2，请输入私钥</div>
				</div>

				<button id="encryptBtn">加密</button>
				<button id="clearBtn">清空</button>
			</div>

			<div>
				<label>加密结果:</label>
				<div id="result" class="result"></div>
			</div>
		</div>

		<script>
			document.addEventListener('DOMContentLoaded', function() {
				const inputText = document.getElementById('inputText');
				const encryptionMethod = document.getElementById('encryptionMethod');
				const keyContainer = document.getElementById('keyContainer');
				const keyInput = document.getElementById('keyInput');
				const encryptBtn = document.getElementById('encryptBtn');
				const clearBtn = document.getElementById('clearBtn');
				const result = document.getElementById('result');

				// 显示/隐藏密钥输入框
				encryptionMethod.addEventListener('change', function() {
					const method = this.value;
					if (method === 'aes' || method === 'sm4' || method === 'sm2') {
						keyContainer.style.display = 'block';
					} else {
						keyContainer.style.display = 'none';
					}
				});

				// 加密按钮点击事件
				encryptBtn.addEventListener('click', function() {
					const text = inputText.value.trim();
					if (!text) {
						alert('请输入要加密的文本!');
						return;
					}

					const method = encryptionMethod.value;
					const key = keyInput.value.trim();

					try {
						let encryptedResult = '';

						switch (method) {
							case 'md5':
								encryptedResult = CryptoJS.MD5(text).toString();
								break;

							case 'sha256':
								encryptedResult = CryptoJS.SHA256(text).toString();
								break;

							case 'aes':
								if (!key) {
									alert('AES加密需要密钥!');
									return;
								}
								encryptedResult = aesEncrypt(text, key);
								break;

							case 'sm3':
								encryptedResult = sm3Encrypt(text);
								break;

							case 'sm4':
								if (!key) {
									alert('SM4加密需要密钥!');
									return;
								}
								encryptedResult = sm4Encrypt(text, key);
								break;

							case 'sm2':
								if (!key) {
									alert('SM2加密需要私钥!');
									return;
								}
								encryptedResult = sm2Encrypt(text, key);
								break;

							default:
								encryptedResult = '不支持的加密方式';
						}

						result.textContent = encryptedResult;
					} catch (error) {
						result.textContent = '加密出错: ' + error.message;
						console.error(error);
					}
				});

				// 清空按钮点击事件
				clearBtn.addEventListener('click', function() {
					inputText.value = '';
					keyInput.value = '';
					result.textContent = '';
				});

				// AES加密函数
				function aesEncrypt(text, key) {
					const keySize = key.length;
					if (keySize !== 16 && keySize !== 24 && keySize !== 32) {
						throw new Error('AES密钥长度应为16/24/32字节 (128/192/256位)');
					}

					const keyBytes = CryptoJS.enc.Utf8.parse(key);
					const iv = CryptoJS.enc.Utf8.parse(key.substring(0, 16)); // 使用密钥前16字节作为IV

					const encrypted = CryptoJS.AES.encrypt(text, keyBytes, {
						iv: iv,
						mode: CryptoJS.mode.CBC,
						padding: CryptoJS.pad.Pkcs7
					});

					return encrypted.toString();
				}

				// SM3加密函数
				function sm3Encrypt(text) {
					// 使用sm-crypto库的SM3实现
					return sm3(text);
				}

				// SM4加密函数
				function sm4Encrypt(text, key) {
					const keySize = key.length;
					if (keySize !== 16) {
						throw new Error('SM4密钥长度应为16字节 (128位)');
					}

					// 使用sm-crypto库的SM4实现
					const keyBytes = utils.utf8ToArray(key);
					const iv = utils.utf8ToArray(key.substring(0, 16)); // 使用密钥前16字节作为IV

					const encrypted = sm4.encrypt(
						utils.utf8ToArray(text),
						keyBytes, {
							iv: iv
						}
					);

					return utils.arrayToHex(encrypted);
				}

				// SM2加密函数
				function sm2Encrypt(text, privateKey) {
					// 使用sm-crypto库的SM2实现
					// 注意: 这里演示的是签名，因为SM2是非对称加密，通常用于签名/验签
					// 如果需要完整的加密/解密功能，需要更复杂的实现

					try {
						const sigValue = sm2.doSignature(text, privateKey);
						return utils.arrayToHex(sigValue);
					} catch (error) {
						throw new Error('SM2加密失败，请检查私钥格式是否正确');
					}
				}
			});
		</script>
	</body>
</html>
